from mem0 import MemoryClient
from dotenv import load_dotenv
import os

'''
运行前需要先安装 mem0ai SDK
`pip install mem0ai`
'''

load_dotenv()
api_key = os.getenv('MEM0_API_KEY')

client = MemoryClient(api_key=api_key)

print("Adding memory...")
messages = [
    {"role": "user", "content": "Thinking of making a sandwich. What do you recommend?"},
    {"role": "assistant", "content": "How about adding some cheese for extra flavor?"},
    {"role": "user", "content": "Actually, I don't like cheese."},
    {"role": "assistant", "content": "I'll remember that you don't like cheese for future recommendations."}
]

# 显式设置output_format和version参数来避免弃用警告
result = client.add(messages, user_id="boby", output_format="v1.1", version="v2")
print(f"Memory added: {result}")

# Example showing location and preference-aware recommendations
print("Searching memory...")
query = "I'm craving some pizza. Any recommendations?"
filters = {
    "AND": [
        {
            "user_id": "boby"  # Changed to match the user_id we used above
        }
    ]
}
search_result = client.search(query, output_format="v1.1", version="v2", filters=filters)
print(f"Search result: {search_result}")

print("Getting all memories...")
filters = {
   "AND": [
      {
         "user_id": "boby"  # Changed to match the user_id we used above
      }
   ]
}

all_memories = client.get_all(output_format="v1.1", version="v2", filters=filters, page=1, page_size=50)
print(f"All memories: {all_memories}")

print("Demo completed successfully!")